/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bpmlab.shift.dao;

import bpmlab.shift.dao.service.EtapaDAO;
import bpmlab.shift.dao.service.GenericDAO;
import bpmlab.shift.entity.Etapa;
import bpmlab.shift.entity.Localizacao;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author Mikael Lima
 */
public class EtapaDAOJpa implements EtapaDAO {

    private final GenericDAO<Etapa> dao = DAOFactory.criarGenericDAO(Etapa.class);

    @Override
    public void salvar(Etapa o) throws DAOException {
        dao.salvar(o);
    }

    @Override
    public void excluir(Etapa o) throws DAOException {
        dao.excluir(o);
    }

    @Override
    public Etapa obter(Object id) {
        return dao.obter(id);
    }

    @Override
    public List<Etapa> obterTodos() {
        return dao.obterTodos();
    }

    @Override
    public List<Etapa> obterPorLocalizacao(Localizacao localizacao) {
        EntityManager manager = JpaUtil.getEntityManager();
        String jqpl = "select e from Etapa e where :localizacao member of e.localizacoes";
        return manager.createQuery(jqpl)
                .setParameter("localizacao", localizacao)
                .getResultList();
    }

    @Override
    public List<Etapa> obterTodosComLocalizacoes() {
        EntityManager manager = JpaUtil.getEntityManager();
        String jqpl = "select distinct e from Etapa e left join fetch e.localizacoes";
        return manager.createQuery(jqpl)
                .getResultList();
    }
    
}
